package com.hisense.hiioc.hismartplate.robot.factory;

import com.hisense.hiioc.hismartplate.constants.DirectiveType;
import com.hisense.hiioc.hismartplate.constants.RobotConstant;
import com.hisense.hiioc.hismartplate.entity.dto.MesRobot;
import com.hisense.hiioc.hismartplate.robot.message.*;
import com.hisense.hiioc.hismartplate.utils.StringUtil;
import com.hisense.hiioc.hismartplate.utils.UUIDGenerator;

import java.util.List;

/**
 * @Auther:biexiande@hisense.com
 * @date:2018/12/17 10:33
 * @des
 */
public class DirectiveFactory {

    /**
     * 生成指令列表
     */


    public static void generateSetSessionIdDirective(List<String> directives,String sessionId){
        Header header = new Header(DirectiveType.SCREEN_RENDER_SETSESSIONID,sessionId);
        header.setMessageId(sessionId);
        SetSessionPayload payload =new SetSessionPayload(sessionId);
        String voiceRecognizeDirect = StringUtil.getDirectiveJson(new Directive(header, payload));
        directives.add(voiceRecognizeDirect);
    }

    public static void buildDirectiveFromRobot(List<String> directiveList,
                                               MesRobot mesRobot,
                                               String token,
                                               RobotResponse robotResponse,
                                               String sessionId){
        String scene = robotResponse.getScene();
        if(robotResponse.isEnd()){
            switch (scene){
                case RobotConstant.SCENE_NAVIGATION_NAVIGATE:
                    generateNavigationDirective(directiveList, mesRobot,token, robotResponse.getParam1(), sessionId);
                    break;
                default:
                    break;
            }
        }else{
            sessionId = UUIDGenerator.create32Key();
            CacheFactory.INSTANCE.getCache().put(sessionId,scene);
            generateTextAndVoiceOutputDirective(directiveList, token, robotResponse.getResponse(), sessionId);
        }
    }

    public static void generateNavigationDirective (List<String> directives,
                                                    MesRobot mesRobot,
                                                    String token,
                                                    String destination,
                                                    String sessionId){
        Header navigationHeader=new Header(DirectiveType.SCREEN_RENDER_TEXTCARD,sessionId);
        String url = String.format(RobotConstant.DIRECTION_URL,mesRobot.getCoordinate(),destination);
        String textContent = String.format(RobotConstant.NAVIGATION_CONTENT,destination);
        NavigationCardPayload navigationCardPayload =new NavigationCardPayload();
        LinkStructure linkStructure = new LinkStructure();
        linkStructure.setUrl(url);
        linkStructure.setAnchorText("查看更多");
        navigationCardPayload.setLink(linkStructure);
        navigationCardPayload.setToken(token);
        navigationCardPayload.setContent(textContent);
        navigationCardPayload.setType(DirectiveType.SCREEN_RENDER_TEXTCARD.getType());
        String navigationDirect = StringUtil.getDirectiveJson(new Directive(navigationHeader,navigationCardPayload));
        directives.add(navigationDirect);
        generateSetSessionIdDirective(directives, "");
    }


    public static void generateDuerosDiective(List<String> directives,String token,String content,String sessionId){

        Header voiceRecognizeHeader = new Header(DirectiveType.SCREEN_RENDER_VOICERECOGNIZECARD,sessionId);
        VoiceRecognizePayload voiceRecognizePayload = new VoiceRecognizePayload();
        voiceRecognizePayload.setContent(content);
        voiceRecognizePayload.setToken(token);
        String voiceRecognizeDirect = StringUtil.getDirectiveJson(new Directive(voiceRecognizeHeader, voiceRecognizePayload));
        directives.add(voiceRecognizeDirect);
    }



    public static void generateTextAndVoiceOutputDirective(List<String> directives,String token,String msgbody ,String sessionId){
        Header textHeader = new Header(DirectiveType.SCREEN_RENDER_TEXTCARD,sessionId);
        TextCardPayload textCardPayload = new TextCardPayload();
        textCardPayload.setContent(msgbody);
        textCardPayload.setType(DirectiveType.SCREEN_RENDER_TEXTCARD.getType());
        textCardPayload.setToken(token);
        String textDirect =StringUtil.getDirectiveJson(new Directive(textHeader, textCardPayload));
        directives.add(textDirect);
        Header voiceHeader = new Header(DirectiveType.VOICE_SPEAK,sessionId);
        SpeakPayload speakPayload = new SpeakPayload();
        speakPayload.setToken(token);
        speakPayload.setContent(msgbody);
        String speakDirect = StringUtil.getDirectiveJson(new Directive(voiceHeader, speakPayload));
        directives.add(speakDirect);
        generateSetSessionIdDirective(directives, sessionId);
    }

}